package com.example.sangong.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.example.sangong.entity.Card;
import com.example.sangong.entity.CardGroup;
import com.example.sangong.entity.Player;
import com.example.sangong.myenum.CardEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j

public class CardGroupUtils {


    /**
     * 没人三张牌
     */
    private static final int CARD_NUM = 3;

    /**
     * 两副牌
     */
    private static final int CARDS_NUM = 2;

    /**
     * 初始化牌组
     */
    public static CardGroup build() {
        CardGroup cardGroup = new CardGroup();
        CardEnum[] values = CardEnum.values();

        List<Card> tempCardList = new ArrayList<>();
        //两副牌
        for (int i = 0; i < CARDS_NUM; i++) {
            for (CardEnum value : values) {
                Card card = new Card();
                card.setCardEnum(value);
                tempCardList.add(card);
            }
        }

        List<Card> cards = random(tempCardList);
        cardGroup.setNoUsedList(cards);
        return cardGroup;
    }


    /**
     * 给玩家发牌
     *
     * @param playerList 玩家们
     * @param cutCardNum 切牌号码
     * @param cutCardNum 牌组
     */
    public static void sendCard(List<Player> playerList, int cutCardNum, CardGroup cardGroup) {

        //发三轮
        List<Card> noUsedList = cardGroup.getNoUsedList();
        List<Card> usedList = cardGroup.getUsedList();
        //牌不够，洗牌
        if (CollectionUtil.isEmpty(noUsedList)) {
            throw new RuntimeException("CARD_OVER");
//            washCard(cardGroup);
        }
        int count = cutCardNum;
        for (int j = 0; j < CARD_NUM; j++) {
            for (int i = 0; i < playerList.size(); i++) {
                int index = count % playerList.size();
                Player player = playerList.get(index);
                List<Card> playerCardList = Optional.ofNullable(player.getCardList()).orElse(new ArrayList<>());
                //发牌核心操作
                Card card = noUsedList.remove(0);
                playerCardList.add(card);
                usedList.add(card);
                player.setCardList(playerCardList);
                count++;
            }

        }

    }

    public static void recycleCard(CardGroup cardGroup) {
        log.info("回收牌了！");
        cardGroup.getThrowList().addAll(cardGroup.getUsedList());
        cardGroup.getUsedList().clear();
    }

    /**
     * 洗牌
     */
    public static void washCard(CardGroup cardGroup) {
        if (CollectionUtil.isNotEmpty(cardGroup.getNoUsedList())) {
            throw new RuntimeException("还有牌没发完，不用洗牌");
        }
        List<Card> throwList = cardGroup.getThrowList();
        if(CollectionUtil.isEmpty(throwList)){
            throwList.addAll(cardGroup.getUsedList());
        }
        List<Card> newCardList = random(throwList);
        //正常洗牌的时候，card-group为空
        cardGroup.getNoUsedList().addAll(newCardList);
    }

    private static List<Card> random(List<Card> cardList) {
        List<Card> cards = new ArrayList<>();
        //开始随机生成牌组
        for (int i = 0, len = cardList.size(); i < len; i++) {
            int random = RandomUtil.randomInt(0, cardList.size());
            cards.add(cardList.remove(random));
        }
        return cards;
    }

    /**
     * 切牌
     */
    public static int cutCard() {
        return RandomUtil.randomInt(1, 13);
    }
}
